library(readr)
library(tidyverse)
library(stargazer)
library(fixest)
library(kableExtra)
library(plotly)
setwd("E:/data/mkt")
#1-1
obesity <- read.csv("obesity.csv",header=T)
obesity<-obesity[,c(1,3,6,7)]
colnames(obesity)[1]<-"country"
measured<-subset(obesity, SUBJECT=="MEASURED")
self<-subset(obesity, SUBJECT=="SELFREPORTED")
stargazer(measured,
type = 'text',
digits=2, align=T,median = T,
title = "Measured",
keep = 'Value')
##
## Measured
## =================================================================
## Statistic N Mean St. Dev. Min Pctl(25) Median Pctl(75) Max
## -----------------------------------------------------------------
## Value 177 48.54 17.64 18.60 30.10 55.90 62.50 75.20
## -----------------------------------------------------------------
stargazer(self,
type = 'text',
digits=2, align=T,median = T,
title = "Self-reported",
keep = 'Value')
##
## Self-reported
## =================================================================
## Statistic N Mean St. Dev. Min Pctl(25) Median Pctl(75) Max
## -----------------------------------------------------------------
## Value 348 46.34 8.71 13.00 42.00 46.80 51.60 66.60
## -----------------------------------------------------------------
OECD<-unique(obesity$country)
cpi<-read.csv("CPI.csv",header=T)
# subset annual data, prices of food, measured by annual growth rate
cpi<-subset(cpi, FREQUENCY=="A" & SUBJECT=="FOOD" & MEASURE =="AGRWTH")
cpi<-cpi[,c(1,6,7)]
colnames(cpi)[1]<-"country"
# subset OECD countries
cpi<-subset(cpi,country %in% OECD)
stargazer(cpi,
type = 'text',
digits=2, align=T,median = T,
title = "CPI",
keep = 'Value')
##
## CPI
## ====================================================================
## Statistic N Mean St. Dev. Min Pctl(25) Median Pctl(75) Max
## --------------------------------------------------------------------
## Value 1,802 7.35 24.88 -13.85 1.28 3.30 6.97 773.99
## --------------------------------------------------------------------
#1-3
obes<-aggregate(measured$Value,by=list(year=measured$TIME),mean)
colnames(obes)[2]<-"measured"
obes$measured<-round(obes$measured,0)
ggplot(obes, aes(x = year,y = measured)) +
geom_segment( aes(x=year, xend=year, y=0, yend=measured), color="grey") +
geom_point( color="orange", size=4) +
theme_light() +
theme(
panel.grid.major.x = element_blank(),
panel.border = element_blank(),
axis.ticks.x = element_blank()
) +
geom_text(data = obes,aes(x = year,y = measured,label =measured),nudge_y = 5, nudge_x = 0.05)+
xlab("year") +
ylab("average measured obesity rate(%)")

# 3
coke<-read.csv("coca.csv")[,c(1,4)]
# 3-1
colnames(cpi)[3]<-"CPI"
colnames(measured)[4]<-"obesity"
cpi2<-aggregate(cpi$CPI,by=list(year=cpi$TIME),mean)
measured2<-aggregate(measured$obesity,by=list(year=measured$TIME),mean)
colnames(cpi2)[2]<-"CPI"
colnames(measured2)[2]<-"obesity"
cpi_obesity2<-merge(cpi2, measured2, by="year")
g<-ggplot(cpi_obesity2, aes(x=CPI, y=obesity,colour=year)) +
geom_point()+
theme_bw()
ggplotly(g)
# 3-2
obesity_coke<-merge(measured, na.omit(coke), by="country",all = T)
g<-ggplot(obesity_coke, aes(x=kj, y=obesity,colour=country)) +
geom_point()+
theme_bw()
ggplotly(g)
# 3-3
dat<-merge(obesity_coke, cpi,by=c("country","TIME"),all=T)
dat<-na.omit(dat)
fit1 = feols(obesity~log(kj), dat)
fit2 = feols(obesity~log(kj) | TIME, dat)
fit3 = feols(obesity~CPI , dat)
fit4 = feols(obesity~CPI | TIME, dat)
fit5 = feols(obesity~CPI | country, dat)
fit6 = feols(obesity~CPI | TIME+country, dat)
x<-etable(fit1 , fit2,fit3, fit4,fit5,fit6,
cluster = "country", headers = c("without","time","without","time","country","time+country"))
kable(x, "html") %>%
kable_styling(full_width = F)
|
|
fit1
|
fit2
|
fit3
|
fit4
|
fit5
|
fit6
|
|
|
without
|
time
|
without
|
time
|
country
|
time+country
|
|
Dependent Var.:
|
obesity
|
obesity
|
obesity
|
obesity
|
obesity
|
obesity
|
|
|
|
|
|
|
|
|
|
(Intercept)
|
2,826.2*** (439.3)
|
|
46.95*** (9.202)
|
|
|
|
|
log(kj)
|
-428.7*** (67.94)
|
-405.5*** (60.85)
|
|
|
|
|
|
CPI
|
|
|
0.3898 (1.035)
|
1.703 (1.360)
|
-0.8970** (0.2525)
|
-0.3275 (0.2495)
|
|
Fixed-Effects:
|
——————
|
—————–
|
—————-
|
————-
|
——————
|
—————-
|
|
TIME
|
No
|
Yes
|
No
|
Yes
|
No
|
Yes
|
|
country
|
No
|
No
|
No
|
No
|
Yes
|
Yes
|
|
_______________
|
__________________
|
_________________
|
________________
|
_____________
|
__________________
|
________________
|
|
S.E.: Clustered
|
by: country
|
by: country
|
by: country
|
by: country
|
by: country
|
by: country
|
|
Observations
|
170
|
170
|
170
|
170
|
170
|
170
|
|
R2
|
0.65104
|
0.74912
|
0.00474
|
0.30853
|
0.93922
|
0.97908
|
|
Within R2
|
–
|
0.66148
|
–
|
0.06697
|
0.22710
|
0.04688
|
library(xml2)
library(dplyr)
library(rvest)
website <- read_html('https://www.basspro.com/shop/en/centerfire-pistol')
guns <- website %>% html_elements("section")
guns
names <- guns %>%
html_nodes(".product_name a") %>%
html_text2()
names
prices <- guns %>%
html_nodes(".price span") %>%
html_text2()
prices